package cn.tedu.property.management.service.impl;

import cn.tedu.property.management.ex.ServiceException;
import cn.tedu.property.management.mapper.ChargeDetailMapper;
import cn.tedu.property.management.mapper.ChargeItemMapper;
import cn.tedu.property.management.mapper.CommunityMapper;
import cn.tedu.property.management.mapper.OwnerMapper;
import cn.tedu.property.management.pojo.dto.ChargeDetailAddNewDTO;
import cn.tedu.property.management.pojo.dto.ChargeDetailUpdateDTO;
import cn.tedu.property.management.pojo.vo.ChargeDetailStandardVO;
import cn.tedu.property.management.pojo.entity.ChargeDetail;
import cn.tedu.property.management.service.IChargeDetailService;
import cn.tedu.property.management.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 处理收费详情的业务实现类
 *
 * @author zyl
 * @version 0.0.1
 */
@Slf4j
@Service
public class ChargeDetailServiceImpl implements IChargeDetailService {

    @Autowired
    private ChargeDetailMapper chargeDetailMapper;

    @Autowired
    private ChargeItemMapper chargeItemMapper;

    @Autowired
    private OwnerMapper ownerMapper;

    @Override
    public void addNew(ChargeDetailAddNewDTO chargeDetailAddNewDTO) {
        log.debug("开始处理添加费用详情的功能！参数为：{}",chargeDetailAddNewDTO);
        Long chargeItemId = chargeDetailAddNewDTO.getChargeItemId();

        //判断是否有该id的费用类型
        if (chargeItemMapper.selectById(chargeItemId) == null){
            String massage = "添加失败，没有该费用项！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        //判断是否有该业主
        if(ownerMapper.getOwnerById(chargeDetailAddNewDTO.getOwnerId())==null){
            String massage = "添加失败，没有该业主！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        ChargeDetail chargeDetail = new ChargeDetail();
        BeanUtils.copyProperties(chargeDetailAddNewDTO,chargeDetail);
        chargeDetailMapper.addNew(chargeDetail);
        log.debug("添加收费详情成功！");
    }

    @Override
    public void deleteByChargeItemId(Long chargeItemId) {
        log.debug("开始处理按照费用项目删除费用详情的功能！费用项目id为：{}",chargeItemId);
        if (chargeItemMapper.selectById(chargeItemId) == null){
            String massage = "删除失败，不存在该收费项目！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        chargeDetailMapper.deleteByChargeItemId(chargeItemId);
        log.debug("删除成功！");
    }

    @Override
    public void update(Long id, ChargeDetailUpdateDTO chargeDetailUpdateDTO) {
        log.debug("开始处理按照id修改费用详情的功能！id为：{}，数据为：{}",id,chargeDetailUpdateDTO);
        if (chargeDetailMapper.selectById(id)==null){
            String massage = "修改失败，不存在该费用详情！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        chargeDetailMapper.update(id, chargeDetailUpdateDTO);
        log.debug("修改成功！");
    }

    @Override
    public ChargeDetailStandardVO selectByStandardByChargeItemId(Long chargeItemId) {
        log.debug("开始处理按照费用项目id查询费用详情的功能！id为：{}，", chargeItemId);
        if (chargeItemMapper.selectById(chargeItemId)==null){
            String massage = "查询失败，不存在该费用项目！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        ChargeDetailStandardVO chargeDetailStandardVO = chargeDetailMapper
                .selectByStandardByChargeItemId(chargeItemId);
        log.debug("查询成功！数据为：{}",chargeDetailStandardVO);
        return chargeDetailStandardVO;
    }

    @Override
    public ChargeDetailStandardVO selectByStandardByOwnerId(Long ownerId) {
        log.debug("开始处理按照业主id查询费用详情的功能！id为：{}，", ownerId);
        //判断该业主是否存在
        if (ownerMapper.getOwnerById(ownerId)==null){
            String massage = "查询失败，不存在该业主！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        ChargeDetailStandardVO chargeDetailStandardVO = chargeDetailMapper
                .selectByStandardByOwnerId(ownerId);
        log.debug("查询成功！数据为：{}",chargeDetailStandardVO);
        return chargeDetailStandardVO;
    }
}
